package com.tencreat.service.mall.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tencreat.common.core.domain.model.ReqIdParam;
import com.tencreat.common.core.domain.model.ReqIdsParam;
import com.tencreat.common.exception.CustomException;
import com.tencreat.domain.mall.MallGoods;
import com.tencreat.domain.mall.MallGoodsNorms;
import com.tencreat.domain.mall.MallOrderGoods;
import com.tencreat.mapper.mall.MallGoodsNormsMapper;
import com.tencreat.service.mall.IMallGoodsNormsService;
import com.tencreat.service.mall.IMallGoodsService;
import com.tencreat.service.mall.IMallOrderGoodsService;
import com.tencreat.service.mall.IMallOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 商品规格参数Service业务层处理
 *
 * @author tencreat
 * @date 2022-01-08
 */
@Service
public class MallGoodsNormsServiceImpl extends ServiceImpl<MallGoodsNormsMapper, MallGoodsNorms> implements IMallGoodsNormsService {


    @Autowired
    MallGoodsNormsMapper goodsNormsMapper;



    @Autowired
    private IMallOrderGoodsService mallOrderGoodsService;



    /**
     * 查询商品规格参数
     *
     * @param id 商品规格参数ID
     * @return 商品规格参数
     */
    @Override
    public MallGoodsNorms selectMallGoodsNormsById(ReqIdParam id) {
        return baseMapper.selectById(id.getId());
    }

    /**
     * 查询商品规格参数列表
     *
     * @param mallGoodsNorms 商品规格参数
     * @return 商品规格参数
     */
    @Override
    public List<MallGoodsNorms> selectMallGoodsNormsList(MallGoodsNorms mallGoodsNorms) {
        return baseMapper.selectList(Wrappers.query(mallGoodsNorms));
    }

    /**
     * 新增商品规格参数
     *
     * @param mallGoodsNorms 商品规格参数
     * @return 结果
     */
    @Override
    public int insertMallGoodsNorms(MallGoodsNorms mallGoodsNorms) {
        int sort = goodsNormsMapper.getNewSortOrder();
        mallGoodsNorms.setCreateTime(LocalDateTime.now());
        mallGoodsNorms.setSortOrder(sort);
        return baseMapper.insert(mallGoodsNorms);
    }

    /**
     * 修改商品规格参数
     *
     * @param mallGoodsNorms 商品规格参数
     * @return 结果
     */
    @Override
    public int updateMallGoodsNorms(MallGoodsNorms mallGoodsNorms) {
        mallGoodsNorms.setUpdateTime(LocalDateTime.now());
        return baseMapper.updateById(mallGoodsNorms);
    }

    /**
     * 批量删除商品规格参数
     *
     * @param ids 需要删除的商品规格参数ID
     * @return 结果
     */
    @Override
    public int deleteMallGoodsNormsByIds(ReqIdsParam ids) {
        for (Long id : ids.getId()) {
//            MallGoodsNorms mallGoodsNorms = baseMapper.selectById(id);
            //根据规格id找到相关订单
            List<MallOrderGoods> mallOrderGoods = mallOrderGoodsService.lambdaQuery().eq(MallOrderGoods::getNormsId, id).list();
            if (!mallOrderGoods.isEmpty()) {
                throw new CustomException("该规格已产生订单");
            }
        }
        return baseMapper.deleteBatchIds(ids.getId());
    }

    /**
     * 删除商品规格参数信息
     *
     * @param id 商品规格参数ID
     * @return 结果
     */
    @Override
    public int deleteMallGoodsNormsById(ReqIdParam id) {
        return baseMapper.deleteById(id.getId());
    }

    @Override
    public Long getFirstNormsId(Long goodsId) {
        QueryWrapper<MallGoodsNorms> wrapper = new QueryWrapper<>();
        wrapper.eq("goods_id", goodsId);
        wrapper.orderByAsc("sort_order");
        wrapper.last(" limit 1");
        return baseMapper.selectOne(wrapper).getId();
    }

    @Override
    public MallGoodsNorms getFirstNorms(Long goodsId) {
        QueryWrapper<MallGoodsNorms> wrapper = new QueryWrapper<>();
        wrapper.eq("goods_id", goodsId);
        wrapper.orderByAsc("sort_order");
        wrapper.last(" limit 1");
        return baseMapper.selectOne(wrapper);
    }

    @Override
    public int reSortOrderReduce(MallGoodsNorms mallGoodsNorms) {
        //查出上一条数据的ID
        Long sourceId = goodsNormsMapper.getUpId(mallGoodsNorms.getSortOrder(), mallGoodsNorms.getGoodsId());
        //两个ID替换
        goodsNormsMapper.exchange(mallGoodsNorms.getId(), sourceId);
        return 0;
    }

    @Override
    public int reSortOrderPlus(MallGoodsNorms mallGoodsNorms) {
        //查出下一条数据的ID
        Long sourceId = goodsNormsMapper.getDownId(mallGoodsNorms.getSortOrder(), mallGoodsNorms.getGoodsId());
        //两个ID替换
        goodsNormsMapper.exchange(mallGoodsNorms.getId(), sourceId);
        return 0;
    }
}
